package com.ruoyi.web.controller.deal;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.deal.domain.HyDealCategory;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.deal.bo.HyDealCategoryAddBo;
import com.ruoyi.deal.bo.HyDealCategoryEditBo;
import com.ruoyi.deal.bo.HyDealCategoryQueryBo;
import com.ruoyi.deal.service.IHyDealCategoryService;
import com.ruoyi.deal.vo.HyDealCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类管理Controller
 *
 * @author lixin
 * @date 2021-05-13
 */
@Api(value = "分类管理控制器" , tags = {"分类管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/deal/category")
public class HyDealCategoryController extends BaseController {


	@Autowired
	private IHyDealCategoryService dealCategoryService;

	/**
	 * 查询分类管理列表
	 */
	@ApiOperation("查询分类管理列表")
	@PreAuthorize("@ss.hasPermi('deal:category:list')")
	@GetMapping("/list")
	public AjaxResult list(HyDealCategoryQueryBo bo) {
		List<HyDealCategoryVo> list = dealCategoryService.queryList(bo);
		return AjaxResult.success(list);
	}

	/**
	 * 获取分类管理详细信息
	 */
	@ApiOperation("获取分类管理详细信息")
	@PreAuthorize("@ss.hasPermi('deal:category:query')")
	@GetMapping("/{categoryId}")
	public AjaxResult<HyDealCategoryVo> getInfo(@PathVariable("categoryId") Integer categoryId) {
		return AjaxResult.success(dealCategoryService.queryById(categoryId));
	}

	/**
	 * 获取分类下拉树列表
	 */
	@GetMapping("/treeselect")
	public AjaxResult treeselect() {

		HyDealCategoryQueryBo bo = new HyDealCategoryQueryBo();
		List<HyDealCategoryVo> categoriesVo = dealCategoryService.queryList(bo);
		List<HyDealCategory> categories = BeanUtil.copyToList(categoriesVo, HyDealCategory.class);
		return AjaxResult.success(dealCategoryService.buildCategoryTreeSelect(categories));
	}

	/**
	 * 加载对应品牌分类列表树
	 */
	@GetMapping(value = "/brandCategoryTreeselect/{brandId}")
	public AjaxResult roleMenuTreeselect(@PathVariable("brandId") Long brandId) {
		List<HyDealCategory> categories = dealCategoryService.list();
		Map<String, Object> ajax = new HashMap<>();
		ajax.put("checkedKeys" , dealCategoryService.selectCategoryListByBrandId(brandId));
		ajax.put("categories" , dealCategoryService.buildCategoryTreeSelect(categories));
		return AjaxResult.success(ajax);
	}

	/**
	 * 新增分类管理
	 */
	@ApiOperation("新增分类管理")
	@PreAuthorize("@ss.hasPermi('deal:category:add')")
	@Log(title = "分类管理" , businessType = BusinessType.INSERT)
	@PostMapping()
	public AjaxResult<Void> add(@RequestBody HyDealCategoryAddBo bo) {
		return toAjax(dealCategoryService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改分类管理
	 */
	@ApiOperation("修改分类管理")
	@PreAuthorize("@ss.hasPermi('deal:category:edit')")
	@Log(title = "分类管理" , businessType = BusinessType.UPDATE)
	@PutMapping()
	public AjaxResult<Void> edit(@RequestBody HyDealCategoryEditBo bo) {
		return toAjax(dealCategoryService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除分类管理
	 */
	@ApiOperation("删除分类管理")
	@PreAuthorize("@ss.hasPermi('deal:category:remove')")
	@Log(title = "分类管理" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryId}")
	public AjaxResult<Void> remove(@PathVariable Integer categoryId) {
		if (dealCategoryService.hasChildByCategoryId(categoryId)) {
			return AjaxResult.error("存在下级分类,不允许删除");
		}
		return toAjax(dealCategoryService.deleteWithValidById(categoryId, true) ? 1 : 0);
	}

	/**
	 * 导出分类管理列表
	 */
	@ApiOperation("导出分类管理列表")
	@PreAuthorize("@ss.hasPermi('deal:category:export')")
	@Log(title = "分类管理" , businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<HyDealCategoryVo> export(HyDealCategoryQueryBo bo) {
		List<HyDealCategoryVo> list = dealCategoryService.queryList(bo);
		ExcelUtil<HyDealCategoryVo> util = new ExcelUtil<HyDealCategoryVo>(HyDealCategoryVo.class);
		return util.exportExcel(list, "分类管理");
	}
}
